package com.malou.client.seguridad.usuario;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.ListViewEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.CheckBoxListView;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.google.gwt.core.client.GWT;
import com.malou.client.MalouConstants;

public class RolesDialog extends Dialog {

	public HiddenField<String> usuario_id;
	private Button cancelar;
	private Button guardar;
	public Status status;
	public UsuarioPanel usuarioPanel;
	private FormBinding formBindings;
	private CheckBoxListView<BeanModel> view;
	private FormPanel panel;
	private MalouConstants constants = GWT.create(MalouConstants.class);

	public RolesDialog() {
		super();
		setModal(true);
		setButtons("");
		setResizable(false);
		setIconStyle("icon-update");
		setHeading(constants.usuarioModificarGrupo());
		setSize(280, 310);

		panel = new FormPanel();
		panel.setBorders(false);
		panel.setBodyBorder(false);
		panel.setPadding(5);
		panel.setHeaderVisible(false);
		panel.setLabelWidth(110);

		formBindings = new FormBinding(panel, false);

		usuario_id = new HiddenField<String>();
		usuario_id.setVisible(false);
		panel.add(usuario_id);

		addList();

	
		status.hide();
		getButtonBar().enable();
	}

	protected void createButtons() {
		super.createButtons();

		if (constants == null) {
			constants = GWT.create(MalouConstants.class);
		}

		status = new Status();
		status.setBusy(constants.guardando());
		status.hide();
		status.setAutoWidth(true);
		getButtonBar().add(status);

		getButtonBar().add(new FillToolItem());

		cancelar = new Button(constants.cancelar(),
				IconHelper.createPath("images/icons/cancel.png"));
		cancelar.setSize(70, 25);
		cancelar.addSelectionListener(new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) {
				RolesDialog.this.hide();
			}

		});

		guardar = new Button(constants.guardar(),
				IconHelper.createPath("images/icons/save_all.png"));
		guardar.setSize(70, 25);

		guardar.addSelectionListener(new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) {

				if (validate()) {
					status.show();
					getButtonBar().disable();

					List<Integer> roles_id = new ArrayList<Integer>();
					for (BeanModel b : view.getChecked()) {
						Integer rol_id = b.get("parent_id");
						roles_id.add(rol_id);
					}

					Usuarios.user_id = Integer.valueOf(usuario_id.getValue());
					Usuarios.modificarRoles(roles_id);

				}
			}
		});

		addButton(guardar);
		addButton(cancelar);

	}

	private void addList() {

		Usuarios.listRoles();
		Usuarios.rolesDialog = this;
		view = new CheckBoxListView<BeanModel>();

		view.setStore(Usuarios.storeListView);
		view.setDisplayProperty("usuario");
		view.setLoadingText(constants.cargando());

		view.addListener(Events.Attach,
				new Listener<ListViewEvent<BeanModel>>() {

					@Override
					public void handleEvent(ListViewEvent<BeanModel> be) {
						Usuarios.loaderListView.load();
					}

				});
		panel.add(view);
		
		add(panel);

	}

	public void setChecked() {

		Integer count = Usuarios.storeListView.getCount();
		for (Integer i = 0; i < count; i++) {
			Integer id = Usuarios.storeListView.getAt(i).get("id");
			if (id != null)
				view.setChecked(Usuarios.storeListView.getAt(i), true);
		}
	}

	public void reset() {
		usuario_id.clear();
		status.hide();
		getButtonBar().enable();
	}

	public void clear() {
		view.getStore().removeAll();
	}

	public void setStore(TreeStore<Usuario> store) {

		formBindings.addFieldBinding(new FieldBinding(usuario_id, "id"));
		formBindings.setStore(store);
	}

	protected boolean validate() {
		boolean retorno = false;
		Integer checked = view.getChecked().size();
		if (checked == 0)
			MessageBox.alert(constants.sistemaMarcocruzado(),
					constants.usuarioMinimoRoles(), null);
		else
			retorno = true;

		return retorno;
	}

	public void bind(Usuario selectedModel) {
		formBindings.bind(selectedModel);
	}

	public void unbind() {
		formBindings.unbind();
	}
}
